package fr.uvsq.isty.bdh.hchess.gui.menu;

import fr.uvsq.isty.bdh.hchess.engine.GameData;
import fr.uvsq.isty.bdh.hchess.gui.MainWindow;
import ictk.boardgame.AmbiguousMoveException;
import ictk.boardgame.IllegalMoveException;
import ictk.boardgame.chess.ChessGame;
import ictk.boardgame.io.InvalidGameFormatException;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ReplayMenu extends JPanel{
	
	//Attributes
	
	/**
	 * The parent windows
	 */
	private MainWindow MW;
	
	/**
	 * The Background picture
	 */
	private Image bg;
	
	/**
	 * Bottom panel, where the back and start button are.
	 */
	private JEmptyPanel bottom;
	
	/**
	 * The button to start
	 */
	private JButton start;
	/**
	 * The button to go back to the menu.
	 */
	private JButton back;
	
	/**
	 * The pgn file to load
	 */    
    private File PGNFile;
    
    /**
     * The explorer
     */
    private JFileChooser fileChooser;
    
    /**
     * Button to access the explorer
     */
    private JButton choosePGN;
    
    
	/**
	 * Bottom panel, where the back and start button are.
	 */
	private JEmptyPanel middle;
	
	
	public ReplayMenu(MainWindow parent)
	{
		this.MW = parent;
		
		//Background
		try {
			bg = ImageIO.read(getClass().getClassLoader()
                                .getResource("menu/menu.jpg"));
		} catch (IOException e1) {		
			e1.printStackTrace();
		}
		

		//middle area
		middle = new JEmptyPanel();
		
		choosePGN = new JButton("Choose a PGN file...");
		
		fileChooser = new JFileChooser();
		choosePGN.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        int returnVal = fileChooser.showOpenDialog(ReplayMenu.this);
 
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            PGNFile = fileChooser.getSelectedFile();
                        } else {
                            // Cancel by user. Do nothing.
                        }
                    }   
                });
		
		middle.add(choosePGN, BorderLayout.CENTER);
		
		middle.setAlignmentX(Component.CENTER_ALIGNMENT);
		this.add(Box.createVerticalGlue());
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));		
		this.add(middle);
		
		//Bottom buttons (back & start)
		bottom = new JEmptyPanel();		

		//Button Back
		back = new JButton("Retour");		
			
		bottom.add(back, BorderLayout.PAGE_START);
		
		back.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	MW.setContentPane(new MainMenu(MW));
            	MW.setVisible(true);
            	MW.repaint();
            }
        });
		
		//Button Start
		start = new JButton("Lancer la partie");	
		
		bottom.add(start, BorderLayout.PAGE_END);
		
		start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ChessGame g = null;
            	if(PGNFile != null)
            	{

                    try {
                        g = GameData.loadGameFromPGN(new FileReader(PGNFile));
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(null, "PGN file invalid.", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                    if (g != null) {
                        MW.getGameData().startReplay(g);
                    }

            	}
            	else
            	{ JOptionPane.showMessageDialog(null, "You have to choose a file faggot !", "ERROR", JOptionPane.ERROR_MESSAGE); }
            	
            	
            }
        });		
		
		//Bottom area
		this.add(bottom, BorderLayout.PAGE_END);
		

	}
	
	/**
	 * PaintComponent, only for the background.
	 */
	 @Override
	    public void paintComponent(Graphics g){
		g.drawImage(bg, 0, 0, this.getWidth(), this.getHeight(), this);
	 }
}
